/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */

package com.jav;

import com.jav.inter.Constantes;
import com.jav.per.Persistencia;
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordEnumeration;


public class CapMovil extends MIDlet implements Constantes{
    private static Form main;
    private Image imgError;
    private Command cm_salir;
    private static VentanaEspera waiting = null;

    public void startApp() {

      Display.init(this);
      Resources r = null;
      Resources r2 = null;
      
        try {      
            r = Resources.open("/res/Wood.res");
            r2 = Resources.open("/res/r.res");
        } catch (IOException ex) {            
            ex.printStackTrace();
        }
      imgError = r2.getImage("cancel.png");
     
      UIManager.getInstance().setThemeProps(r.getTheme("Cookbook"));
      //if (isLicenciaOK()){
          cm_salir = new Command("Salir",r2.getImage("exit.png"))
          {
                public void actionPerformed(ActionEvent evt) {
                    notifyDestroyed();
                }

          };



          Form inicio = new InicoForm();
          Image img = r2.getImage("fondo");
          inicio.setBgImage(img);
          inicio.addCommand(cm_salir);
          inicio.addCommand(new Command("Inicio", r2.getImage("ok.png")){
                public void actionPerformed(ActionEvent evt) {
                    new Thread(new Runnable() {

                    public void run() {
                          waiting = new VentanaEspera("INICIANDO...");
                          waiting.showModeless();
                          main = new Menu();
                          waiting.dispose();
                          main.show();
                          main.addCommand(cm_salir);
                    }
                }).start();
                      
                }

          });
          inicio.show();
     
    }

    public static Form getMainForm(){
        return main;
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    private boolean isLicenciaOK() {
        
        int i = getNumVeces();        
        if (i  > 100){
            return false;
        }
        else{
            try {
                savenumVeces(i + 1);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return true;
        }



    }

    public int getNumVeces(){
        Persistencia p = null;
        int valor = 1;
        try {
            p = new Persistencia(PERS_LICENCIA);
            RecordEnumeration re = p.getRecordStore().enumerateRecords(null,null,false);
            while (re.hasNextElement()){
                ByteArrayInputStream bin = new ByteArrayInputStream(re.nextRecord());
                DataInputStream din = new DataInputStream(bin);
                valor = din.readInt();
                bin.close();
                din.close();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        finally{
            if (p != null){
                try {
                    p.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return valor;
    }
   public void savenumVeces(int num) throws IOException {
        Persistencia p = null;

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        try{
            dout.writeInt(num);
            dout.flush();
            p = new Persistencia(PERS_LICENCIA);
            p.save(bout.toByteArray());

        }
        catch (Exception ex) {
                ex.printStackTrace();
        }
        finally{
            if(dout!= null){
                 dout.close();
            }
            if(bout!= null){
                 bout.close();
            }
        }


    }
}
